package top.lixunda.ecommerce.server.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.user.api.controller.ISystemMenuController;
import top.lixunda.ecommerce.server.user.api.entity.db.SystemMenuDO;
import top.lixunda.ecommerce.server.user.api.entity.dto.SystemMenuChangeDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.SystemMenuInsertDTO;
import top.lixunda.ecommerce.server.user.api.entity.vo.SystemMenuTreeVO;
import top.lixunda.ecommerce.server.user.api.entity.vo.SystemMenuVO;
import top.lixunda.ecommerce.server.user.service.ISystemMenuService;

import java.util.List;

/**
 * 系统菜单管理接口
 *
 * @author lixunda
 * @version time:2020/4/26 9:52
 */
@Slf4j
@RestController
public class SystemMenuController extends BaseController
        implements ISystemMenuController {

    private final ISystemMenuService systemMenuService;

    @Autowired
    public SystemMenuController(ISystemMenuService systemMenuService) {
        this.systemMenuService = systemMenuService;
    }

    /**
     * 添加多个菜单目录
     *
     * @param list 新添加的菜单目录数据列表
     * @return 是否添加成功
     */
    @Override
    public MessageObject<Boolean> insertMenu(List<SystemMenuInsertDTO> list) {
        boolean result = systemMenuService.insertMenu(list);
        return result ? getSuccessMessageObject("添加菜单成功！", true)
                : getErrorMessageObject("添加菜单失败！", false);
    }

    /**
     * 更新多个菜单目录
     *
     * @param list 需要更新的菜单目录数据列表
     * @return 是否更新成功
     */
    @Override
    public MessageObject<Boolean> updateMenuById(List<SystemMenuChangeDTO> list) {
        boolean result = systemMenuService.updateMenuById(list);
        return result ? getSuccessMessageObject("更新菜单成功！", true)
                : getErrorMessageObject("更新菜单失败！", false);
    }

    /**
     * 根据id删除菜单目录，删除包括子节点的所有节点
     *
     * @param id 菜单目录id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeMenuAllById(Integer id) {
        boolean result = systemMenuService.removeMenuAllById(id);
        return result ? getSuccessMessageObject("删除菜单成功！", true)
                : getErrorMessageObject("删除菜单失败！", false);
    }

    /**
     * 根据id删除菜单目录，删除包括子节点的所有节点
     *
     * @param ids 菜单目录id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeMenuAllByIds(List<Integer> ids) {
        boolean result = systemMenuService.removeMenuAllByIds(ids);
        return result ? getSuccessMessageObject("删除菜单成功！", true)
                : getErrorMessageObject("删除菜单失败！", false);
    }

    /**
     * 根据id获取菜单目录数据
     *
     * @param id 菜单目录id
     * @return 单条菜单目录数据
     */
    @Override
    public MessageObject<SystemMenuVO> queryById(Integer id) {
        SystemMenuDO systemMenuDO = systemMenuService.queryById(id);
        SystemMenuVO result = SystemMenuVO.fromSystemMenuDO(systemMenuDO);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 查询所有菜单目录信息
     *
     * @return 所有菜单目录信息
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryAll() {
        List<SystemMenuDO> list = systemMenuService.queryAll();
        List<SystemMenuVO> result = SystemMenuVO.fromSystemMenuDO(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 查询所有菜单目录信息树形结构
     *
     * @return 所有菜单目录信息树形结构
     */
    @Override
    public MessageObject<List<SystemMenuTreeVO>> queryAllTree() {
        List<SystemMenuDO> list = systemMenuService.queryAll();
        List<SystemMenuTreeVO> result = SystemMenuTreeVO.fromSystemMenuToTree(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 根据菜单编码查询某一个子系统的菜单目录
     *
     * @param menuCode 菜单编码，唯一索引
     * @return 该子系统的菜单目录信息
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryOneRootAllByCode(String menuCode) {
        List<SystemMenuDO> list = systemMenuService.queryOneRootAllByCode(menuCode);
        List<SystemMenuVO> result = SystemMenuVO.fromSystemMenuDO(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 根据菜单编码查询某一个子系统的菜单目录树形结构数据
     *
     * @param menuCode 菜单编码，唯一索引
     * @return 该子系统的菜单目录信息树形结构数据
     */
    @Override
    public MessageObject<SystemMenuTreeVO> queryOneRootAllTreeByCode(String menuCode) {
        List<SystemMenuDO> list = systemMenuService.queryOneRootAllByCode(menuCode);
        List<SystemMenuTreeVO> result = SystemMenuTreeVO.fromSystemMenuToTree(list);
        if (ObjectValidators.isEmpty(result)) {
            return getErrorMessageObject("查询菜单失败！结果为空！", null);
        } else {
            return getSuccessMessageObject("查询菜单成功！", result.get(0));
        }
    }

    /**
     * 根据父节点id获取子类节点列表
     *
     * @param id 父节点id
     * @return 子节点列表
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryChildrenByParentId(Integer id) {
        List<SystemMenuDO> list = systemMenuService.queryChildrenByParentId(id);
        List<SystemMenuVO> result = SystemMenuVO.fromSystemMenuDO(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 查询首层节点
     *
     * @return 首层节点
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryRoots() {
        List<SystemMenuDO> list = systemMenuService.queryRoots();
        List<SystemMenuVO> result = SystemMenuVO.fromSystemMenuDO(list);
        return getSuccessMessageObject("查询首层菜单成功！", result);
    }
}
